﻿using System;
using System.Net;

namespace FindProxy
{
    public class WebDownload : WebClient
    {

        public WebDownload(int timeout = 10000)
        {
            this._timeout = timeout;
        }
        private int _timeout;
        private System.Threading.Timer t;
        /// <summary>     /// Time in milliseconds     /// </summary>   
        public int Timeout
        {
            get { return _timeout; }
            set
            {
                _timeout = value;
            }
        }

        public void GetStringAsync(Uri uri, int timeout = 10000)
        {
            this.Timeout = timeout;
            this.DownloadStringAsync(uri);

            t = new System.Threading.Timer(
                      s =>
                      {
                          if (this.IsBusy)
                          {
                              this.CancelAsync();
                          }
                      }, null, Timeout, System.Threading.Timeout.Infinite
                );
        }
        protected override WebRequest GetWebRequest(Uri address)
        {
            var result = base.GetWebRequest(address);
            result.Timeout = this._timeout;
            return result;
        }


    }

    public class DownloadEventArgs
    {

        public DownloadEventArgs(string v, int pi)
        {
            this.VisitURI = v;
            this.ProxyIndex = pi;
        }
        public int ProxyIndex { get; set; }
        public string VisitURI { get; set; }
    }
}
